package com.diors.commons.network.module;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import org.jboss.netty.buffer.ChannelBuffer;

import com.diors.commons.network.DiorsBuffer;
import com.diors.commons.network.module.model.CommandInfoModel;

/**
 * 解析命令
 * @author	darren.ouyang
 * @data	2013-9-11
 */
public class Resolve 
{
	static int TYPE_BYTE = 0;
	
	static int TYPE_SHORT = 1;
	
	static int TYPE_INT = 2;
	
	static int TYPE_LONG = 3;
	
	static int TYPE_FLOAT = 4;
	
	static int TYPE_DOUBLE = 5;
	
	static int TYPE_STRING = 6;
	
	static int TYPE_BOOLEAN = 7;
	
	static int TYPE_OTHER = 8;
	/**
	 * 收到包的解析
	 * @param client
	 * @param buffer
	 * @param commands
	 * @return
	 * @throws Exception 
	 */
	public static void resolveReceivable(Object[] objs, DiorsBuffer buffer, CommandInfoModel[] commands)
	{
		/**从那一个位置开始**/
		int i = 1;
		for(CommandInfoModel model : commands)
		{
			objs[i] = model.getTypeId().get().read(buffer.getBuffer(), model);
			i += 1;
		}
	}
	
	/**
	 * 解析发送出去的数据
	 * @param objs
	 * @param buffer
	 * @param commands
	 * @throws UnsupportedEncodingException 
	 */
	public static void resolveSendable(Object obj,ChannelBuffer buffer, CommandInfoModel command)
	{
		command.getTypeId().get().write(buffer, obj, command);
	}
	
	/**
	 * 、解析类型接口
	 * @author	darren.ouyang
	 * @data	2013-9-11
	 */
	private interface IResolveBuffer 
	{
		/**
		 * 读
		 * @param buffer
		 * @return
		 */
		public Object read(ByteBuffer buffer, CommandInfoModel model);
		
		/**
		 * 读
		 * @param buffer
		 * @return
		 */
		public Object read(ChannelBuffer buffer, CommandInfoModel model);
		
		/**
		 * 写
		 * @param buffer
		 * @param value
		 */
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model);
	}
	
	/**
	 * 数据类型枚举
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	public enum BuffType
	{
		BYTE(new ByteResolveBuffer()),
		SHORT(new ShortResolveBuffer()),
		INT(new IntResolveBuffer()),
		LONG(new LongResolveBuffer()),
		FLOAT(new FloatResolveBuffer()),
		DOUBLE(new DoubleResolveBuffer()),
		STRING(new StringResolveBuffer()),
		BOOLEAN(new BooleanResolveBuffer()),
		OTHER(new OtherResolveBuffer());
		
		private IResolveBuffer resolve;
		
		private BuffType(IResolveBuffer resolve)
		{
			this.resolve = resolve;
		}
		
		public IResolveBuffer get()
		{
			return resolve;
		}
		
		public static BuffType getType(String str)
		{
			BuffType type;
			
			if("byte".equalsIgnoreCase(str))
			{
				type = BuffType.BYTE;
			}
			else if("short".equalsIgnoreCase(str))
			{
				type = BuffType.SHORT;
			}
			else if("int".equalsIgnoreCase(str))
			{
				type = BuffType.INT;
			}
			else if("long".equalsIgnoreCase(str))
			{
				type = BuffType.LONG;
			}
			else if("float".equalsIgnoreCase(str))
			{
				type = BuffType.FLOAT;
			}
			else if("double".equalsIgnoreCase(str))
			{
				type = BuffType.DOUBLE;
			}
			else if("string".equalsIgnoreCase(str))
			{
				type = BuffType.STRING;
			}
			else if("boolean".equalsIgnoreCase(str))
			{
				type = BuffType.BOOLEAN;
			}
			else
			{
				type = BuffType.OTHER;
			}
			
			return type;
		}
	}
	
	
	/**
	 * byte型解析
	 * @author 杜祥
	 * @create 2013
	 */
	private static class ByteResolveBuffer implements IResolveBuffer
	{
		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model)
		{
			return buffer.get();
		}
		
		public Object read(ChannelBuffer buffer, CommandInfoModel model)
		{
			return buffer.readByte();
		}
		

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model)
		{
			buffer.writeByte((Integer)value);
		}
	}
	
	/**
	 * short型解析
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	private static class ShortResolveBuffer implements IResolveBuffer
	{

		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model)
		{
			return buffer.getShort();
		}

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model) 
		{
			buffer.writeShort((Integer)value);
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model)
		{
			return buffer.readShort();
		}
	}
	
	/**
	 * int型解析
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	private static class IntResolveBuffer implements IResolveBuffer
	{

		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model) 
		{
			return buffer.getInt();
		}

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model)
		{
			buffer.writeInt((Integer) value);
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model) 
		{
			return buffer.readInt();
		}
	}
	
	/**
	 * long型解析
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	private static class LongResolveBuffer implements IResolveBuffer
	{

		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model)
		{
			return buffer.getLong();
		}

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model) 
		{
			buffer.writeLong((Long) value);
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model)
		{
			return buffer.readLong();
		}
	}
	
	
	/**
	 * float型数据解析
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	private static class FloatResolveBuffer implements IResolveBuffer
	{

		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model) {
			return buffer.getFloat();
		}

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model)
		{
			buffer.writeFloat((Float) value);
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model)
		{
			return buffer.readFloat();
		}
		
	}
	
	
	/**
	 * double型数据解析
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	private static class DoubleResolveBuffer implements IResolveBuffer
	{
		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model) 
		{
			return buffer.getDouble();
		}

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model) 
		{
			buffer.writeDouble((Double) value);
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model)
		{
			return buffer.readDouble();
		}
	}
	
	/**
	 * 字符串数据解析
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	private static class StringResolveBuffer implements IResolveBuffer
	{

		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model)
		{
			StringBuilder tb = new StringBuilder();
			for (char c; (c = buffer.getChar()) != 0;)
			{
				tb.append(c);
			}
			String str = tb.toString();
			return str;
		}

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model)
		{
			String str = (String) value;
			for(int i = 0 ; i < str.length() ; i++)
			{
				buffer.writeChar(str.charAt(i));
			}
			buffer.writeChar('\000');
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model)
		{
			StringBuilder tb = new StringBuilder();
			for (char c; (c = buffer.readChar()) != 0;)
			{
				tb.append(c);
			}
			String str = tb.toString();
			return str;
		}
		
	}
	
	/**
	 * boolean型数据解析
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	private static class BooleanResolveBuffer implements IResolveBuffer
	{

		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model)
		{
			return buffer.get() == 0 ? Boolean.FALSE : Boolean.TRUE;
		}

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model)
		{
			buffer.writeByte(((Boolean)value) ? 1 : 0);
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model) 
		{
			return buffer.readByte() == 0 ? Boolean.FALSE : Boolean.TRUE;
		}
	}
	
	
	/**
	 * 其他类型数据解析
	 * @author 杜祥
	 * @create 2013-7-8
	 */
	private static class OtherResolveBuffer implements IResolveBuffer
	{

		@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public void write(ChannelBuffer buffer, Object value,
				CommandInfoModel model) {
			// TODO Auto-generated method stub
			
		}

		/*@Override
		public Object read(ByteBuffer buffer, CommandInfoModel model) 
		{
			return BufferManager.getReceivable(model.getType()).read(buffer);
		}

		@Override
		public void write(ChannelBuffer buffer, Object value, CommandInfoModel model) 
		{
			BufferManager.getSendable(model.getType()).write(buffer, value);
		}

		@Override
		public Object read(ChannelBuffer buffer, CommandInfoModel model)
		{
			return BufferManager.getReceivableN(model.getType()).read(buffer);
		}*/
		
	}
}
